package com.spark.lola.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.constant.CertificationStatus;
import com.spark.lola.entity.SupplierToken;
import com.spark.lola.entity.pur.OrderInfo;
import com.spark.lola.entity.sup.BankInfo;
import com.spark.lola.mapper.sup.BankInfoMapper;
import com.spark.lola.service.sup.IBankInfoService;
import com.spark.lola.support.common.constant.BooleanConstant;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收款方式
 * @Author: DengYeJun
 * @Date: 2020/5/22 14:26
 */
@Api(value = "/api/bank/", tags = "供应商收款管理")
@RequestMapping("/api/bank/")
@RestController
public class BankController {
    @Autowired
    private BankInfoMapper bankInfoMapper;
    @Autowired
    private IBankInfoService bankInfoService;

    @ApiOperation(value = "查询收款列表")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseBean index(@SessionAttribute SupplierToken tokenUser){
        QueryWrapper<BankInfo> query =new QueryWrapper<>();
        query.eq("supplier_id",tokenUser.getSupplierId());
        query.eq("used", CommonConstant.Y);
        query.orderByDesc("auto");
        List<BankInfo> lst = bankInfoMapper.selectList(query);
        return ResponseBean.success(lst);
    }

    @ApiOperation(value = "收款详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.POST)
    public ResponseBean detail(@PathVariable Long id,@SessionAttribute SupplierToken tokenUser){
        BankInfo bankInfo =new BankInfo();
        if(id.equals(0L)){
            bankInfo.setSupplierId(tokenUser.getSupplierId());
        }else{
            bankInfo = bankInfoMapper.selectById(id);
        }
        return ResponseBean.success(bankInfo);
    }

    @ApiOperation(value = "保存/更新收款信息")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody BankInfo form,@SessionAttribute SupplierToken tokenUser){
        if(!tokenUser.getCertificationStatus().equals(CertificationStatus.SUCCESS)){
            return ResponseBean.error("未通过实名认证!");
        }
        BankInfo model = new BankInfo();
        model.setSupplierId(tokenUser.getSupplierId());
        if (form.getId() != null) {
            // 禁止修改项 做空处理
            model = bankInfoService.getById(form.getId());
        }
        DataBeanUtils.buildUpdateBean(model, form);
        if(model.getAuto().equals(BooleanConstant.TRUE)){
            // 默认收款方式 重置其他收款信息
            bankInfoMapper.resetAuto(model.getSupplierId());
        }
        bankInfoService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "指定默认收款信息")
    @RequestMapping(value = "/auto/{id}",method = RequestMethod.POST)
    public ResponseBean auto(@PathVariable Long id,@SessionAttribute SupplierToken tokenUser) {
        bankInfoMapper.resetAuto(tokenUser.getSupplierId());
        bankInfoMapper.setAuto(id);
        return ResponseBean.success();
    }

    @ApiOperation(value = "删除收款信息")
    @RequestMapping(value = "/del/{id}",method = RequestMethod.POST)
    public ResponseBean del(@PathVariable Long id) {
        bankInfoService.removeById(id);
        return ResponseBean.success();
    }

}
